﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;
using System.Data;

namespace EWebsite.Admin.Products {
    public partial class Edit : System.Web.UI.Page {
        private DataTable tblProduct;

        public DataTable TblProduct {
            get { return tblProduct; }
        }

        private DataTable _tblCategories;
        public DataTable TblCategories { get { return this._tblCategories; } }

        int productID;
        protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack) {
                try {
                    productID = int.Parse(Request["p"]);
                    hdID.Value = productID.ToString();
                    if (productID <= 0)
                        Response.Redirect("Default.aspx");
                    _DataBind();

                }
                catch (Exception ex) {
                    Response.Redirect("Default.aspx");
                }
            }
        }

        protected void _DataBind() {
            CategoryBuss buss = new CategoryBuss();
            _tblCategories = buss.getCategories();
                        
            ProductBuss pbuss = new ProductBuss();
            tblProduct = pbuss.getProduct(productID);

            txtName.Text = TblProduct.Rows[0]["ProductName"].ToString();
            txtDescription.Text = TblProduct.Rows[0]["ProductDescription"].ToString();
            txtOtherInfo.Text = TblProduct.Rows[0]["OtherInfo"].ToString();
            txtPrice.Text = TblProduct.Rows[0]["ProductPrice"].ToString();
            txtQuantityAtLeast.Text = TblProduct.Rows[0]["QuantityAtLeast"].ToString();
            txtHeight.Text = TblProduct.Rows[0]["Height"].ToString();
            txtLength.Text = TblProduct.Rows[0]["Length"].ToString();
            txtWeight.Text = TblProduct.Rows[0]["Weight"].ToString();
            txtWidth.Text = TblProduct.Rows[0]["Width"].ToString();
            txtInstock.Text = TblProduct.Rows[0]["InStock"].ToString();
            imgProduct.ImageUrl = TblProduct.Rows[0]["ProductPhotoURL"].ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            if (IsPostBack) {
                try {
                    String photoUrl = "";
                    int catID = int.Parse(Request["categoryTree"]);
                    String name = txtName.Text.Trim();
                    String description = txtDescription.Text.Trim();
                    String otherInfo = txtOtherInfo.Text;
                    //String otherInfo = Request["txtOtherInfo"].Trim();
                    float price = float.Parse(txtPrice.Text);
                    float width = float.Parse(txtWidth.Text);
                    float height = float.Parse(txtHeight.Text);
                    float length = float.Parse(txtLength.Text);
                    float weight = float.Parse(txtWeight.Text);
                    int Instock = int.Parse(txtInstock.Text);
                    int quantityAtLeast = int.Parse(txtQuantityAtLeast.Text);
                    //int id = int.Parse(Request["productID"]);
                    int id = int.Parse(hdID.Value);

                    if (fuImage.HasFile) {
                        if (Helper.isImageAllow(fuImage.FileName)) { //if is image
                            String fileName = Helper.convertToSlug(fuImage.FileName);
                            //upload
                            fuImage.SaveAs(Server.MapPath("~") + "images/uploads/" + fileName);
                            photoUrl = "/images/uploads/" + fileName;
                        }
                    }

                    ProductBuss buss = new ProductBuss();
                    buss.update(id, name, catID, price, photoUrl, description, otherInfo, Instock, width, height, length, weight, quantityAtLeast);

                    productID = id;
                    _DataBind();
                }
                catch (Exception ex) {
                    return;
                }
            }
        }
    }
}